<?php
namespace GuessNewSong\Controller;
use Common\Controller\GuessNewSongController;

class IndexController extends GuessNewSongController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    //获取题目
    public function new_problem()
    {
        $uid        =   I('post.uid','','htmlspecialchars');
        $share_uid  =   I('post.share_uid','','htmlspecialchars');

        $appid = I('post.appid','','htmlspecialchars');

        if (! empty($uid))
        {
            $data           =   array();
            //做频繁访问或刷数据处理
            $redis = new \Redis();
            $redis->connect('127.0.0.1',6379);

            $allow_key      =   $appid.'_'.$uid;
            $allow_request  =   $redis->get($allow_key);
            if (! empty($allow_request))
            {
                $this->returnData($data,$msg='服务器繁忙',$code=400);
            }

            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','openid','session_key','game_num','totle_num','play_time','play_num','total_money','money_total');
            $member_data    =   M('newsong_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                //绑定好友
                if (! empty( $share_uid) &&  $share_uid != $uid)
                {
                    $this->share_fraend($share_uid,$uid,$appid);
                }

                $today_time     =   strtotime(date('Y-m-d',time()));

                $isfree = 0;
                $play_num = 12;
                if ($member_data['money_total']>2.75){
                    $play_num = $this->updatetag($uid,$member_data['money_total'],$play_num);
                    if ($play_num==2){ //羊毛党
                        $isfree = 0;
                    }
                }
                //限制用户一天玩的次数
                if ($member_data['play_time'] > $today_time && $member_data['play_num'] >= $play_num)
                {
                    $this->returnData($data,$msg='今天挑战次数已达上限','明天再来',$code=400);
                }

                if ($member_data['game_num'] > 0 || ($member_data['play_time'] < $today_time)|| $member_data['total_money']<1)
                {
                    //减去一次机会
                    $update_data                =   array();
                    if ($member_data['play_time'] >= $today_time&&$member_data['game_num']>0)
                    {
                        $update_data['game_num']    =   $member_data['game_num']    -   1;
                    }
                    $update_data['totle_num']   =   $member_data['totle_num']   +   1;
                    $update_data['update_time'] =   time();

                    if($member_data['play_time'] > $today_time)
                    {
                        $update_data['play_time']   =   time();
                        $update_data['play_num']    =   $member_data['play_num']    +   1;
                    }
                    else
                    {
                        $update_data['play_time']   =   time();
                        $update_data['play_num']    =   1;
                    }

                    M('newsong_member')->where($where)->save($update_data);

                    $data['sharelive'] = 0;//1分享复活0隐藏
                    $data['sharelivetitle'] = "邀请好友";//分享

                    if ($member_data['total_money']==0.00){
                        $data['sharelive'] = 0;//1分享复活0隐藏
                        $data['sharelivetitle'] = "邀请好友";//分享
                    }

                    $data['success_number']     =   5;
                    $data['question']           =   $this->getmanyquersion($uid,5,$appid);


                    $this->returnData($data,$msg='ok',$code=200);
                }
                else
                {
                    $this->returnData($data,$msg='你已经用完机会了',$code=400);
                }
            }
        }

        $this->returnData($data,$msg='用户id参数不能为空',$code=404);
    }

    //回答
    public function new_answer()
    {
        $id         =   I('post.id','','htmlspecialchars');
        $number     =   I('post.number','','htmlspecialchars');
        $aswer_id   =   I('post.aswer_id','','htmlspecialchars');
        $uid        =   I('post.uid','','htmlspecialchars');
        $answer_tg  =   I('post.answer_tg','','htmlspecialchars');

        $appid = I('post.appid','','htmlspecialchars');

        if (!$appid){
            $appid = "wx370386b7eb0c04bc";
        }

        $data       =   array();
        if (! empty($uid) && ! empty($number)
        && ! empty($aswer_id) && ! empty($uid))
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','money_total','openid','prize_num','total_money');
            $member_data    =   M('newsong_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                $question_data  =   S('newsong_questions');
                if (! empty($question_data))
                {
                    $this->cachequesion(3600*24*30);
                    $question_data  =   S('newsong_questions');
                }

                $target_question    =   $question_data[$id];

                if (! empty($target_question))
                {
                    if (! empty($answer_tg))
                    {
                        //答对的做验证
                        $where_record           =   array();
                        $field                  =   array('id','number_1','number_2','number_3','number_4','number_5');
                        $where_record['uid']    =   $uid;
                        $where_record['status'] =   0;
                        $questions_record       =   M('newsong_questions_record')->where($where_record)->field($field)->find();

                        if (! empty($questions_record))
                        {
                            $record_id              =   $questions_record['id'];
                            $pay_status             =   true;

                            if($questions_record['number_5'] != $aswer_id || $questions_record['number_5'] != $answer_tg )
                            {
                                $pay_status     =   false;
                            }
                            else
                            {
                                $where_answer           =   array();
                                $where_answer['id']     =   $id;
                                $field                  =   array('aswer_id');
                                $aswer_data             =   M('newsong_questions')->where($where_answer)->field($field)->find();
                                if (empty($aswer_data))
                                {
                                    $pay_status     =   false;
                                }
                                elseif ($aswer_data['aswer_id'] != $aswer_id || $aswer_data['aswer_id'] != $answer_tg)
                                {
                                    $pay_status     =   false;
                                }
                                else
                                {
                                    $pay_status             =   true;
                                }
                            }

                            M('newsong_questions_record')->where($where_record)->delete();

                            if ($pay_status)
                            {
                                //做防刷处理
                                $where_danger               =   array();
                                $where_danger['member_id']  =   $uid;
                                $where_danger['time']       =   array('gt',time() - 10);
                                $red_bags                   =   M('newsong_redbag')->where($where_danger)->count();

                                if ($red_bags >= 5)
                                {
                                    $redis = new \Redis();
                                    $redis->connect('127.0.0.1',6379);
                                    $allow_key      =   $appid.'_'.$uid;
                                    $redis->set($allow_key,'true',60 * 30); // 30分钟有效
                                    //设置过期时间5分钟 (10分钟)
                                    $danger     =   -1;
                                }

                                //更新通关数
                                $update_data                =   array();
                                $update_data['prize_num']   =   $member_data['prize_num']   +   1;
                                if (! empty($danger))
                                {
                                    $update_data['danger']   =   -1;
                                }
                                M('newsong_member')->where($where)->save($update_data);

                                //获取红包
                                $data['challenge_id']   =   $this->create_redbag($uid,$appid,$record_id);
                                $data['msg']            =   '恭喜你通过所有关卡';
                                $data['type']           =   4;
                                $this->returnData($data,$msg='恭喜你通过所有关卡',$code=200);
                            }
                            else
                            {
                                //发错的
                                $data['challenge_id']   =   0;
                                $data['msg']            =   '回答错误';
                                $data['number']         =   $number;
                                $data['type']           =   2;
                                $this->returnData($data,$msg='回答错误',$code=200);
                            }

                            //更新题卡
                            $where_record['id']    =    $questions_record['id'];
                            $update_data           =    array();
                            $update_data['status'] =    1;
                            M('newsong_questions_record')->where($where_record)->save($update_data);

                        }
                        else
                        {
                            //发错的
                            $data['challenge_id']   =   0;
                            $data['msg']            =   '非法操作';
                            $data['number']         =   $number;
                            $data['type']           =   2;
                            $this->returnData($data,$msg='非法操作',$code=200);
                        }
                    }
                    else
                    {
                        //发错的
                        $data['challenge_id']   =   0;
                        $data['msg']            =   '回答错误';
                        $data['number']         =   $number;
                        $data['type']           =   2;
                        $this->returnData($data,$msg='回答错误',$code=200);
                    }
                }
                else
                {
                    $this->returnData($data,$msg='题目不存在',$code=404);
                }
            }

        }

        $this->returnData($data,$msg='用户id参数不能为空',$code=404);
    }

    //更多好玩
    public function extension()
    {
        $data = M('newsong_app')->where(array('isshow'=>'1'))->order('sort asc')->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];


        }
        $this->returnData($data,$msg='ok',$code=200);
    }

    /**
     * 广告
     */
    public function advertising(){

        $data = M('newsong_ad')->where(array('isshow'=>'1'))->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];

        }

        $this->returnData($data,$msg='ok',$code=200);

    }

    /**
     * 存放模板消息
     */
    public function send()
    {
        $appid = I('post.appid','','htmlspecialchars');
        $touser = I('post.touser','','htmlspecialchars');
        $page = I('post.page','','htmlspecialchars');
        $form_id = I('post.form_id','','htmlspecialchars');
        $form_type = I('post.form_type','','htmlspecialchars');
        $secret = I('post.secret','','htmlspecialchars');
        $member_id = I('post.uid','','int');
        $template_id = I('post.template_id','1','htmlspecialchars');

        if ($touser==""||$form_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
            'uid'=>$member_id,
        );

        M('newsong_template_send')->data($insert)->add();
        $this->returnData('成功','200');



    }

    /**
     * 分享好友
     * @param $share_uid
     * @param $uid
     */
    public function share_fraend($share_uid,$uid,$appid)
    {
        $where          =   array();
        $where['uid']   =   $share_uid;
        $field          =   array('uid');
        $member_data    =   M('newsong_member')->where($where)->field($field)->find();
        if (! empty($member_data))
        {
            $where_relationship                         =   array();
            $where_relationship['my_member_id']         =   $share_uid;
            $where_relationship['coupling_member_id']   =   $uid;
            $field_relationship                         =   array('id');
            $result     =   M('newsong_relationship')->where($where_relationship)->field($field_relationship)->find();

            if (empty($result))
            {
                $insert_data                        =   array();
                $insert_data['my_member_id']        =   $share_uid;
                $insert_data['coupling_member_id']  =   $uid;
                $insert_data['createtime']          =   time();
                $insert_data['appid']               =   $appid;
                M('newsong_relationship')->add($insert_data);
            }
        }
    }

    public function rules()
    {
        $data   =   array();
        $data[] =   '1.  红包模式：每次挑战后获得随机红包，累计金额达到5元可以发起提现。';
        $data[] =   '2.  提现请联系客服，工作人员将定时为您发放红包奖励。';
        $data[] =   '3.  本游戏规则最终解释权归游戏开发者所有。';

        $this->returnData($data,$msg='规则',$code=200);
    }

    /**
     * 活动
     * activity_type 1是机会 2是红包
     */
    public function activity()
    {
        $data           =   array();
        $uid            =   I('post.uid','','int');
        $activity_type  =   I('post.activity_type','','int');
        if (! empty($uid) && ! empty($activity_type) && $activity_type == '1')
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','activity_time','activity_type','game_num');
            $member_data    =   M('newsong_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                if (empty($member_data['activity_time']))
                {
                    $update_data                    =   array();
                    $update_data['game_num']        =   $member_data['game_num']    +   2;
                    $update_data['update_time']     =   time();
                    $update_data['activity_time']   =   time();
                    $update_data['activity_type']   =   1;
                    $result     =   M('newsong_member')->where($where)->save($update_data);

                    if (!empty($result))
                    {
                        $data['num']        =   2;
                        $data['game_num']   =   $update_data['game_num'];
                        $this->returnData($data,$msg='成功领取活动机会！',$code=200);
                    }
                    else
                    {
                        $data['num']        =   0;
                        $data['game_num']   =   $member_data['game_num'];
                        $this->returnData($data,$msg=' 领取机会失败！',$code=400);
                    }
                }
                else
                {
                    $data['num']        =   0;
                    $data['game_num']   =   $member_data['game_num'];
                    $this->returnData($data,$msg='你已经领取过机会了！',$code=400);
                }
            }
        }

        $data['num']        =   0;
        $data['game_num']   =   0;
        $this->returnData($data,$msg='非法请求！',$code=400);
    }

    /**
     * 截屏
     */
    public function getscreen(){

        $appid  = I('post.appid','','htmlspecialchars');
        $uid    = I('post.uid','','int');

        if (!$appid||!$uid){
            $this->returnData('','非法操作',400);
        }
        $data['screen']         = 1;
        $data['screen_time']    = time();
        M('newsong_member')->where(array('uid'=>$uid))->save($data);
        $data['status']         = 1;
        $this->returnData($data);
    }
}